//import 'package:amap_base/amap_base.dart';

import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hotwork/Myapplication.dart';
import 'package:flutter_hotwork/bean/ListDeviceProjectBean.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:flutter_hotwork/xunjian/XunJianWenTiShangBao.dart';

import '../Toast.dart';

class XunJianAreaDeviceActivity2 extends StatefulWidget {
  @override
  baseState createState() => new baseState();
}

class baseState extends State<XunJianAreaDeviceActivity2> {
  ListDeviceProjectBean areaDeviceBean;
  List result;
  List checkTaskDevicesList;

  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
    queryData();
  }

//手机号的控制器
  @override
  void dispose() {
//    _amapLocation.stopLocate();
    MyApplication.areaDeviceBean.clear();
    MyApplication.areaDeviceBean2.clear();
    MyApplication.areaDeviceBean = null;
    MyApplication.areaDeviceBean2 = null;
    super.dispose();
  }

  ScrollController controller = ScrollController();

  ///接收选择的结果

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new BaseViewBar(
          childView: Column(
            children: [
              new BaseTitleBar(
                "检查项目",
                leftIcon: 'images/fanhui.png',
                rightIcon: "",
                rightClick: () {
                  print("点击了右边干嘛啊。。。哦");
                },
                leftClick: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
          preferredSize: Size.fromHeight(58.0)),
      body: checkTaskDevicesList == null
          ? Center(child: Text("数据加载中..."))
          : checkTaskDevicesList.length == 0
              ? Center(child: Text("暂无数据"))
              : Container(
                  padding: EdgeInsets.only(left: 10),
                  child: genContentList(context, controller)),
    );
  }

  Widget genContentList(BuildContext context, controller) {
    return ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemCount: checkTaskDevicesList.length,
        itemBuilder: (context, index) {
          Map itemMap = checkTaskDevicesList[index];
          List itemList = itemMap["checkTaskProjects"];
          return Container(
            margin: EdgeInsets.all(10),
            padding: EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(6.0)),
              boxShadow: [
                BoxShadow(
                    color: Colors.grey.withOpacity(0.7),
                    offset: Offset(0.8, 0.8),
                    blurRadius: 2.4)
              ],
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                GestureDetector(
                  child: Container(
                    width: double.maxFinite,
                    padding: EdgeInsets.all(5),
                    // decoration: BoxDecoration(
                    //     border: Border.all(color: Colors.black, width: 1)),
                    child: Text(
                        "设备名称：${itemMap["deviceName"]}\t类型:${itemMap["deviceTypeName"]}\t编码:${itemMap["deviceCode"]}"),
                  ),
                  onTap: () => setState(() {
                    itemMap["state"] = !itemMap["state"];
                  }),
                ),
                Visibility(
                  visible: itemMap["state"],
                  child: ListView.builder(
                      physics: NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      scrollDirection: Axis.vertical,
                      itemCount: itemList.length,
                      itemBuilder: (context, position) {
                        return Container(
                          margin: EdgeInsets.only(left: 20),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Container(
                                  margin: EdgeInsets.only(top: 10),
                                  child: Text(
                                      "检查项目:${itemList[position]["projectName"]}")),
                              Container(
                                  margin: EdgeInsets.only(top: 10),
                                  child: Text(
                                      "检查内容:${itemList[position]["projectContext"]}")),
                              Container(
                                  margin: EdgeInsets.only(top: 10),
                                  child: Row(children: [
                                    Text("检查结果:"),
                                    itemList[position]["projectQuestionStatus"]
                                                .toString() == "1"
                                        ? Text("异常", style:
                                                TextStyle(color: Colors.green))
                                        : itemList[position]["projectStatus"]
                                                    .toString() == "1"
                                            ? Text("正常",
                                                style: TextStyle(color: Colors.green))
                                            : Text("未检查",
                                                style: TextStyle(color: Colors.red)),
                                  ])),
                              Row(
                                children: [
                                  Expanded(child: Container()),
                                  InkWell(
                                    onTap: () {
                                      // if (itemList[position]["projectStatus"]
                                      //         .toString() == "0") {
                                        checkProject(itemList[position]["id"],result[0]["inspectorId"]);
                                      // } else {}
                                    },
                                    child: Container(
                                        width: 50,
                                        height: 20,
                                        margin: EdgeInsets.only(
                                            left: 10,
                                            right: 0,
                                            top: 0,
                                            bottom: 0),
                                        padding: EdgeInsets.only(
                                            left: 2,
                                            right: 2,
                                            top: 0,
                                            bottom: 0),
                                        decoration:
                                            BoxDecoration(
                                                // border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                                color: Colors.blue, // 底色
                                                borderRadius:
                                                    new BorderRadius.circular(
                                                        (5.0)), // 圆角度
                                              ),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Text("正常",
                                              style: TextStyle(
                                                  color: Colors.white,
                                                  fontSize: 10),
                                            )
                                          ],
                                        )),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          MaterialPageRoute(builder: (context) {
                                        MyApplication.areaDeviceBean =
                                            checkTaskDevicesList[index];
                                        MyApplication.areaDeviceBean2 =
                                            itemList[position];
                                        return new XunJianEditScreen();
                                      }));
                                    },
                                    child: Container(
                                        width: 55,
                                        height: 20,
                                        margin: EdgeInsets.only(
                                            left: 10,
                                            right: 0,
                                            top: 0,
                                            bottom: 0),
                                        padding: EdgeInsets.only(
                                            left: 2,
                                            right: 2,
                                            top: 0,
                                            bottom: 0),
                                        decoration: BoxDecoration(
                                                // border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                                color: Colors.blue, // 底色
                                                borderRadius:
                                                    new BorderRadius.circular(
                                                        (5.0)), // 圆角度
                                              ),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Text(
                                              itemList[position][
                                                              "projectQuestionStatus"]
                                                          .toString() ==
                                                      "0"
                                                  ? "问题上报"
                                                  : "问题已上报",
                                              style: TextStyle(
                                                  color: Colors.white,
                                                  fontSize: 10),
                                            )
                                          ],
                                        )),
                                  ),
                                ],
                              )
                            ],
                          ),
                        );
                      }),
                )
              ],
            ),
          );
        });
  }

//获取项目
  void queryData() async {
    // Navigator.pop(context);
    try {
      Response response;
      response = await DioUtils.dio.get(DioUtils.API_PREFIX +
          "/checkTask/listDeviceProject?areaId=" +
          MyApplication.areaId.toString() +
          "&taskId=" +
          MyApplication.nowtaskbean.id.toString() +
          "&inspectorId=" +
          MyApplication.inspacterId.toString());
      Map respMap = json.decode(response.data);
      areaDeviceBean = ListDeviceProjectBean.fromJson(respMap);
      if (null != areaDeviceBean.result && areaDeviceBean.result.length > 0) {
        MyApplication.deviceId = areaDeviceBean.result[0].id;
      }
      result = respMap["result"];
      checkTaskDevicesList = result[0]["checkTaskDevices"];
      for (var i = 0; i < checkTaskDevicesList.length; i++) {
        Map mapcheck = checkTaskDevicesList[i];
        mapcheck.putIfAbsent("state", () => false);
      }
      setState(() {});
    } catch (e) {
      print('请求出错：' + e.toString());
    }
  }

//修改状态
  void checkProject(int id,int inspectorId) async {
    // Navigator.pop(context);
    try {
      Response response;
      response = await DioUtils.dio.get(
          DioUtils.API_PREFIX + "/checkTask/checkProject?id=" + id.toString()+"&inspectorId="+inspectorId.toString());
      Map respMap = json.decode(response.data);
      if (respMap["code"].toString() == "200") {
        queryData();
      } else {
        LocalToast.toast(context, msg: respMap["message"]);
      }
    } catch (e) {
      print('请求出错：' + e.toString());
    }
  }
}
